﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.ServiceModel.Description;
using System.Threading;
using System.ServiceModel;
using System.Diagnostics;
using System.Configuration.Assemblies;

namespace Sample_App1
{
    public partial class Form1 : Form
    {
        public WCF_Lookup.Service1Client WCF_lu;
        public WS_Lookup.Service1SoapClient WS_lu;
        protected delegate void dlgUpdateLabel(string Value, Label Control);
        protected delegate void dlgUpdatePicture(Image Picture);
        public string connection_string;
        public const string queryconnection = "Data Source=ASDS-MSFT\\;Initial Catalog=WCF_Automate;Integrated Security=True";


        public Form1()
        {
            InitializeComponent();
            WCF_lu = new WCF_Lookup.Service1Client();
            WS_lu = new WS_Lookup.Service1SoapClient();
            //connection_string = System.Configuration.ConfigurationManager.AppSettings["connection_string"];
            connection_string = System.Configuration.ConfigurationSettings.AppSettings["connection_string"];
            

            //MessageBox.Show(connection_string);

        }

        private void WCF_LOS_SelectedIndexChanged(object sender, EventArgs e)
        {
            //WCF_Lookup.Service1Client WCF_lu = new WCF_Lookup.Service1Client();
            string col = WCF_lu.GetData(Convert.ToInt16( WCF_LOS.Text));
            if (col == null)
            {
            }
            else
            {

                string[] col_arr = col.Split(',');
                WCF_FN.Text = col_arr[1];
                WCF_LN.Text = col_arr[2];
                WCF_Age.Text = col_arr[0];
                WCF_RN.Text = col_arr[4];
                WCF_MO.Text = col_arr[3];
            }

        }

        private void WS_LOS_SelectedIndexChanged(object sender, EventArgs e)
        {
            string col = WS_lu.GetData(Convert.ToInt16(WS_LOS.Text));
            if (col == null)
            {
            }
            else
            {

                string[] col_arr = col.Split(',');
                WS_FN.Text = col_arr[1];
                WS_LN.Text = col_arr[2];
                WS_Age.Text = col_arr[0];
                WS_RN.Text = col_arr[4];
                WS_MO.Text = col_arr[3];
            }
        }

     

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'sample_AppDataSet.Sample_App' table. You can move, or remove it, as needed.
            this.sample_AppTableAdapter.Fill(this.sample_AppDataSet.Sample_App);
            WCF_LOS.DataSource = WCF_lu.GetRollNumbers();
            WS_LOS.DataSource = WS_lu.GetRollNumbers();
        }

        private void WCF_Enroll_Student_Click(object sender, EventArgs e)
        {
            WCF_lu.Enroll_Student(WCF_ES_FirstName.Text, WCF_ES_LastName.Text, Convert.ToInt32(WCF_ES_Age.Text), Convert.ToInt32(WCF_ES_MarksObtained.Text));            
            System.Windows.Forms.MessageBox.Show("Student Created");
            WCF_ES_FirstName.Text="";
            WCF_ES_LastName.Text="";
            WCF_ES_Age.Text="";
            WCF_ES_MarksObtained.Text = "";
         }


        
        private void tabPage3_Enter(object sender, EventArgs e)
        {
            WCF_LOS.DataSource = WCF_lu.GetRollNumbers();
        }
        

        private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
        {

        }

        private void WS_Enroll_Student_Click_1(object sender, EventArgs e)
        {
            WS_lu.Enroll_Student(WS_ES_FirstName.Text, WS_ES_LastName.Text, Convert.ToInt32(WS_ES_Age.Text), Convert.ToInt32(WS_ES_MarksObtained.Text));
            System.Windows.Forms.MessageBox.Show("Student Created");
            WS_ES_FirstName.Text = "";
            WS_ES_LastName.Text = "";
            WS_ES_Age.Text = "";
            WS_ES_MarksObtained.Text = "";
        }

        private void tabPage1_Enter_1(object sender, EventArgs e)
        {
            WS_LOS.DataSource = WS_lu.GetRollNumbers();
            
        }

        private void tabPage5_Click(object sender, EventArgs e)
        {
            
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void Update_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "Unload_Image")
            {
                pictureBox1.ImageLocation = "";
                button1.Text = "Load_Image";
            }
            else
            {

                Thread ProcessingThread = new Thread(new ThreadStart(StartCalling_Buffer));
                ProcessingThread.IsBackground = true;
                ProcessingThread.Start();
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Thread ProcessingThread = new Thread(new ThreadStart(StartCalling_Streaming));
            ProcessingThread.IsBackground = true;
            ProcessingThread.Start();
        }

        private void UpdatePicture(Image Picture)
        {
            pictureBox1.Image = Picture;
            pictureBox1.Refresh();
            Picture.Dispose();

            //button1.Visible = false;
            //button1.Text ="Unload_Image";
        }

        private void StartCalling_Buffer()
        {
            pictureBox1.Image = null;
            System.Threading.Thread.Sleep(100);
            LoadImageBuffer(1);
        }

        private void StartCalling_Streaming()
        {
            pictureBox1.Image = null;
            System.Threading.Thread.Sleep(100);
            LoadImageStreamed(1);
        }

        private void LoadImageBuffer(int ImageNumber)
        {

            Uri httpUrl = new Uri("http://localhost:8500/WCFSamples/Streaming/");
            ServiceHost host = new ServiceHost(typeof(Buffered_Response.ServiceImpl), httpUrl);
            host.AddServiceEndpoint(typeof(Buffered_Response.IStreamingService), new BasicHttpBinding("WCFSamples_Buffered"), "");
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            host.Description.Behaviors.Add(smb);
            host.Open();
            Buffered_Response.ServiceImpl ClientHandle = new Buffered_Response.ServiceImpl();

            Stream OutputFile = null;

            try
            {
                OutputFile = ClientHandle.GetData(GetImageName(ImageNumber));

                if (OutputFile != null)
                {
                    System.Drawing.Bitmap RawImage = new Bitmap(OutputFile);
                    OutputFile.Close();
                    this.Invoke(new dlgUpdatePicture(UpdatePicture), RawImage);

                }
            }
            catch (FaultException<IOException> ioExcept)
            {
                MessageBox.Show("An IO Exception occured inside the server: " + ioExcept.Message, "WCF Communication Exception");
            }
            catch (CommunicationException comExcept)
            {
                MessageBox.Show("A WCF Communication Error has Occured: " + comExcept.Message, "WCF Comm Error");
            }
            MessageBox.Show("Image loaded through Buffering");

            host.Close();

        }

        private void LoadImageStreamed(int ImageNumber)
        {

            Uri httpUrl = new Uri("http://localhost:8500/WCFSamples/Streaming/");
            ServiceHost host = new ServiceHost(typeof(Buffered_Response.ServiceImpl), httpUrl);
            host.AddServiceEndpoint(typeof(Buffered_Response.IStreamingService), new BasicHttpBinding("WCFSamples_Streamed"), "");
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            host.Description.Behaviors.Add(smb);
            host.Open();
            Buffered_Response.ServiceImpl ClientHandle = new Buffered_Response.ServiceImpl();

            Stream OutputFile = null;

            try
            {
                OutputFile = ClientHandle.GetData(GetImageName(ImageNumber));

                if (OutputFile != null)
                {
                    System.Drawing.Bitmap RawImage = new Bitmap(OutputFile);
                    OutputFile.Close();
                    this.Invoke(new dlgUpdatePicture(UpdatePicture), RawImage);

                }
            }
            catch (FaultException<IOException> ioExcept)
            {
                MessageBox.Show("An IO Exception occured inside the server: " + ioExcept.Message, "WCF Communication Exception");
            }
            catch (CommunicationException comExcept)
            {
                MessageBox.Show("A WCF Communication Error has Occured: " + comExcept.Message, "WCF Comm Error");
            }

            host.Close();
            MessageBox.Show("Image loaded through Streaming");

        }

        private string GetImageName(int ImageNumber)
        {
            switch (ImageNumber)
            {
                case 0:
                    return "Earth";
                case 1:
                    return "Dryden";
                case 2:
                    return "Helix";
                default:
                case 3:
                    return "Cray2";
            }

        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        
                   
        
    }
}
